package com.example.lending.View.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lending.Model.Favor;
import com.example.lending.R;

import java.util.List;

public class FavorAdapter extends RecyclerView.Adapter<FavorAdapter.MyViewHolder> {
    private Context context;
    private List<Favor> favors;

    public FavorAdapter(Context context, List<Favor> favors) {
        this.context = context;
        this.favors = favors;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if(holder instanceof MyViewHolder){
            final Favor favor = favors.get(position);
            ((MyViewHolder) holder).tvName.setText("书名"+favor.getBookname());
            ((MyViewHolder) holder).tvTime.setText("收藏时间："+favor.getTime());
            if(favor.getState()==1){
                ((MyViewHolder) holder).button.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {//取消收藏

                    }
                });
            }
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_favor,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public int getItemCount() {
        if(favors==null)
            return 0;
        return favors.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvName,tvTime;
        Button button;
        public MyViewHolder(View itemView){
            super(itemView);
            tvName=itemView.findViewById(R.id.tv_namefavor);
            tvTime=itemView.findViewById(R.id.tv_sexfavor);
            button=itemView.findViewById(R.id.btn_shoucangfavor);
        }
    }
}
